from openai import OpenAI
import openai
#from IPython.display import Image,display,Audio,Markdown
from PIL import Image
import base64

image_path = "E:/星球.jpg"
openai.api_key = "sk-6S0PtpNia71gjcfwSsDPsJ9mGqsVPr2XRQzAx1dHbJS7RW4t"
openai.api_base="https://chatapi.littlewheat.com/v1"


client = OpenAI(api_key=openai.api_key,base_url=openai.api_base)
#display(Image(image_path))

#img = Image.open(image_path)
#img.show()

# 将图片进行64位编码
def encode_image(image_path):
    with open(image_path,'rb') as f:
        return base64.b64encode(f.read()).decode("utf-8")

base64_image = encode_image(image_path)

response = client.chat.completions.create(
    model= "gpt-4o",
    messages=[
        {
            "role":"system",
            "content":"You are a helpful assistant that responds in Markdown."
        },
        {
            "role":"user",
            "content":[
                {"type": "text", "text": "对图片内容进行描述"},
                {"type": "image_url", "image_url": {
                    "url": f"data:image/png;base64,{base64_image}"}
                }
            ]
        }
    ]
)
print(response.choices[0].message.content)
